package exteditapp.state;

import exteditapp.AppletIterator;
import exteditapp.FileDefault;
import exteditapp.action.IAction;

/**
 * Interface for standard file state
 * @author Arturas Smorgun, arturas@smorgun.lt
 */
public abstract class IState  {

	/**
	 * Label of state
	 */
	protected String label = "";

	/**
	 * Progress (if state has one) - for instance: download or upload state
	 */
	protected int progress = 0;

	/**
	 * File, which state it is
	 */
	protected FileDefault file;

	protected AppletIterator iterator;
	
	protected int birth = 0;
	
	protected IAction actionMenu = null;
	
	/**
	 * Default constructor
	 * @param iterator is system iterator, which performs all actions
	 * @param file this is file, which state we will hold
	 */
	public IState(AppletIterator iterator, FileDefault file, boolean doRegister) {
		this.start(iterator, file, doRegister);
	}

	public IState(AppletIterator iterator, FileDefault file){
		this.start(iterator, file, true);
	}
	
	protected void start(AppletIterator iterator, FileDefault file, boolean doRegister){
		//iterator and lifetime counter
		this.iterator = iterator;
		if(iterator instanceof AppletIterator)
			this.birth = iterator.getLifetime();

		//working file
		this.file = file;
		
		//If state should be registered as current state of WorkingFile - do it. Otherwise just ignore this state.
		if(doRegister)
			file.setCurrentState(this);
	}
	
	/**
	 * Return label
	 * @return String current label
	 */	
	public String getLabel(){
		return this.label;
	}	

	/**
	 * Set the value of label.
	 * @param mylabel new label value
	 */
	public void setLabel(String mylabel){
		this.label = mylabel;
	}

	/**
	 * Return progress
	 * @return current progress 
	 */	
	public int getProgress(){
		return this.progress;
	}

	/**
	 * Set the value of progress.
	 * @param myprogress new progress value
	 */
	public void setProgress(Integer myprogress){
		this.progress = myprogress;
	}	

	/**
	 * Return file
	 * @return FileDefault file, which state it is now
	 */	
	public FileDefault getFile(){
		return this.file;
	}	

	/**
	 * Set the value of file.
	 * @param myfile new file, which state we will hold
	 */
	public void setFile(FileDefault myfile){
		this.file = myfile;
	}	

	public AppletIterator getIterator(){
		return iterator;
	}
	
	public void setIterator(AppletIterator iterator){
		this.iterator = iterator;
	}
	
	/**
	 * Update current state - increase/decrease of progress bar and change of label
	 * @param label new label value
	 * @param progress new progress value
	 */
	public void update(String label, int progress) {
		//TODO implement state update
	}

	public void resetLifetime(){
		this.birth = this.iterator.getLifetime();
	}
	
	/**
	 * Abstraction of new state handling
	 * @return true on change and false on idle
	 */
	public abstract boolean handle();

	protected int getLifetime(){
		if(iterator instanceof AppletIterator){
			return iterator.getLifetime() - birth;
		}
		return 0;
	}
	
	public void setAction(IAction act){
		this.actionMenu = act;
	}
	
	public IAction getAction(){
		return this.actionMenu;
	}
	
}
